﻿/*
* 作者:haojielyb
* 邮箱:haojielyb@qq.com
*/

using Furion.DependencyInjection;
using MqttModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Lxb.Model.Service
{
    public class DeviceService : ITransient
    {
  
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能



        public DeviceService(ISqlSugarClient db)
        {
            this.db = db.CopyNew();
        }

        /// <summary>
        /// remark
        /// </summary>
        /// <returns></returns>
        public async Task<int> OnlineAsync(Device dev)
        {
           await    db.Updateable<Device>(dev).IgnoreColumns(true).UpdateColumns(new string[] { "status", "lasttime" }).Where(d => d.SN == dev.SN).ExecuteCommandAsync();
            return await Offline();
        }

        public async Task<int> OnlineFacePayAsync(Weixin_dev dev)
        {
        
           await  db.Updateable<Weixin_dev>(dev).IgnoreColumns(true).UpdateColumns(new string[] { "status", "lasttime" }).Where(d => d.Id == dev.Id).ExecuteCommandAsync();
            return await wxOffline();
        }

        /// <summary>
        /// remark
        /// </summary>
        /// <returns></returns>
        public async Task<int> Offline()
        {
            var t = DateTime.Now.AddMinutes(-6).ToString("yyyy/MM/dd HH:mm:ss");
            var sql = $"update  lxb_device  set status =0 where lasttime <='{t}'";
           // Console.WriteLine(sql);
           return await  db.Ado.ExecuteCommandAsync(sql);
        }
        public async Task<int> wxOffline()
        {
            var t = DateTime.Now.AddMinutes(-6).ToString("yyyy/MM/dd HH:mm:ss");
            var sql = $"update  lxb_weixin_dev  set status =0 where lasttime <='{t}'";

            return await db.Ado.ExecuteCommandAsync(sql);
        }
        public void errorlog(List<AddErrInfoItem> snlog, string sn)
        {
            foreach (var item in snlog)
            {
                face_log fl = new face_log();
                fl.uid = Convert.ToInt32(item.customId);
                fl.why = item.errcode;
                fl.sn = sn;
                db.Insertable<face_log>(fl).ExecuteCommand();
            }
        }
    }
}